using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

using SlimDX;

using miciv;
using miciv.Rendering;

namespace micivCore.Data
{
    public class MapTileLayer
    {
        protected MapTileDefinition m_definition;
        public MapTileDefinition Definition
        {
            get
            {
                return this.m_definition;
            }
        }

        protected CoreContext m_coreContext;
        public CoreContext CoreContext
        {
            get
            {
                return this.Definition.CoreContext;
            }
        }

        protected string m_strID;
        public string ID
        {
            get
            {
                return this.m_strID;
            }
        }

        protected Hashtable m_htblModifiers;

        public MapTileLayer(MapTileDefinition _definition)
        {
            this.m_definition = _definition;
            this.m_htblModifiers = new Hashtable();
        }

        public bool Initialize(XmlElement _xmlLayer)
        {
            this.m_strID = this.CoreContext.CoreManager.GetXmlAttributeValue(_xmlLayer, "id", "");
            XmlNodeList lstXmlModifiers = _xmlLayer.SelectNodes("modifiers/modifier");
            foreach (XmlElement xmlModifier in lstXmlModifiers)
            {
                string strID = this.CoreContext.CoreManager.GetXmlAttributeValue(xmlModifier, "id", "");
                string strType = this.CoreContext.CoreManager.GetXmlAttributeValue(xmlModifier, "type", "");
                string strValue = this.CoreContext.CoreManager.GetXmlAttributeValue(xmlModifier, "value", "");
                switch (strType)
                {
                    case "float":
                    {
                        this.m_htblModifiers.Add(strID, float.Parse(strValue));
                        break;
                    }
                    case "int":
                    {
                        this.m_htblModifiers.Add(strID, int.Parse(strValue));
                        break;
                    }
                    case "bool":
                    {
                        this.m_htblModifiers.Add(strID, bool.Parse(strValue));
                        break;
                    }
                    case "uint":
                    {
                        this.m_htblModifiers.Add(strID, uint.Parse(strValue));
                        break;
                    }
                    case "string":
                    {
                        this.m_htblModifiers.Add(strID, strValue);
                        break;
                    }
                    case "rgba":
                    {
                        char[] separator = { ' ' };
                        string[] rgba = strValue.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                        if (4 != rgba.Length)
                        {
                            throw new Exception(string.Format("{1} : {0} rgba type badly formatted.", strID, xmlModifier.OwnerDocument.BaseURI));
                        }
                        this.m_htblModifiers.Add(strID, new Color4(
                            float.Parse(rgba[3]),
                            float.Parse(rgba[0]),
                            float.Parse(rgba[1]),
                            float.Parse(rgba[2])));
                        break;
                    }
                    case "texture":
                    {
                        TextureManager textureManager = this.m_coreContext.GetData<TextureManager>("TextureManager");
                        this.m_htblModifiers.Add(strID, textureManager.GetTexture(strValue));
                        break;
                    }
                }
            }
            return true;
        }

        public bool HasModifier(string _strModifier)
        {
            return this.m_htblModifiers.ContainsKey(_strModifier);
        }

        public object GetModifier(string _strModifier)
        {
            return this.m_htblModifiers[_strModifier];
        }
    }
}
